package com.riku.security.controller;

import com.riku.security.common.SeCommonResponse;
import com.riku.security.common.SnowFlake;
import com.riku.security.model.SeMenu;
import com.riku.security.service.SeMenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created by 1 on 2021/5/18.
 */
@Api(value = "security-menu",description = "权限-菜单")
@RestController
@RequestMapping("/se/menu/")
public class SeMenuController {

    @Autowired
    private SeMenuService seMenuService;

    @Autowired
    private SnowFlake snowFlake;

    @ApiOperation("新增菜单")
    @PostMapping("insertMenu")
    public SeCommonResponse insertMenu(
            @ApiParam(required = true) @RequestParam(required = true) String menuName,
            @ApiParam(required = true) @RequestParam(required = true) Integer menuType,
            @ApiParam(required = false) @RequestParam(required = false) String menuPath,
            @ApiParam(required = false) @RequestParam(required = false) Long parentId,
            @ApiParam(required = true) @RequestParam(required = true) Integer orderNum,
            @ApiParam(required = true) @RequestParam(required = true) String serviceCode,
            @ApiParam(required = true) @RequestParam(required = true) Integer status
    ) {
        SeMenu entity = new SeMenu();
        entity.setId(snowFlake.nextId());
        entity.setMenuName(menuName);
        entity.setMenuType(menuType);
        entity.setMenuPath(menuPath);
        entity.setParentId(parentId);
        entity.setOrderNum(orderNum);
        entity.setServiceCode(serviceCode);
        entity.setStatus(status);
        seMenuService.insertMenu(entity);
        return SeCommonResponse.success();
    }

    @ApiOperation("修改菜单")
    @PutMapping("updateMenu")
    public SeCommonResponse updateMenu(
            @ApiParam(required = true) @RequestParam(required = true) Long id,
            @ApiParam(required = true) @RequestParam(required = true) String menuName,
            @ApiParam(required = true) @RequestParam(required = true) Integer menuType,
            @ApiParam(required = false) @RequestParam(required = false) String menuPath,
            @ApiParam(required = false) @RequestParam(required = false) Long parentId,
            @ApiParam(required = true) @RequestParam(required = true) Integer orderNum,
            @ApiParam(required = true) @RequestParam(required = true) String serviceCode,
            @ApiParam(required = true) @RequestParam(required = true) Integer status
    ) {
        SeMenu entity = new SeMenu();
        entity.setId(id);
        entity.setMenuName(menuName);
        entity.setMenuType(menuType);
        entity.setMenuPath(menuPath);
        entity.setParentId(parentId);
        entity.setOrderNum(orderNum);
        entity.setStatus(status);
        entity.setServiceCode(serviceCode);
        seMenuService.updateMenu(entity);
        return SeCommonResponse.success();
    }

    @ApiOperation("启用/停用菜单")
    @PutMapping("statusMenu")
    public SeCommonResponse statusMenu(
            @ApiParam(required = true) @RequestParam(required = true) Long id,
            @ApiParam(required = true) @RequestParam(required = true) Integer status
    ) {
        SeMenu entity = new SeMenu();
        entity.setId(id);
        entity.setStatus(status);
        seMenuService.updateMenu(entity);
        return SeCommonResponse.success();
    }

    @ApiOperation("删除菜单")
    @DeleteMapping("deleteMenu")
    public SeCommonResponse deleteMenu(
            @ApiParam(required = true) @RequestParam(required = true) Long id
    ) {
        seMenuService.deleteMenu(id);
        return SeCommonResponse.success();
    }

    @ApiOperation("菜单列表")
    @GetMapping("selectMenuPage")
    public SeCommonResponse<List<SeMenu>> selectMenuPage(
            @ApiParam(required = false) @RequestParam(required = false) String menuName,
            @ApiParam(required = true) @RequestParam(required = true) String serviceCode
    ) {
        return SeCommonResponse.success(seMenuService.selectMenuPage(serviceCode,menuName));
    }

    @ApiOperation("菜单接口树")
    @GetMapping("selectMenuTree")
    public SeCommonResponse<List<SeMenu>> selectMenuTree(
            @ApiParam(required = true) @RequestParam(required = true) String serviceCode,
            @ApiParam(required = false) @RequestParam(required = false) Integer includeApi
    ) {
        return SeCommonResponse.success(seMenuService.selectMenuTree(serviceCode,includeApi));
    }

    @ApiOperation("接口列表")
    @GetMapping("selectMenuApiPage")
    public SeCommonResponse<List<SeMenu>> selectMenuApiPage(
            @ApiParam(required = false) @RequestParam(required = false) String menuName,
            @ApiParam(required = false) @RequestParam(required = false) Long parentId,
            @ApiParam(required = true) @RequestParam(required = true) String serviceCode
    ) {
        return SeCommonResponse.success(seMenuService.selectMenuApiPage(serviceCode,parentId,menuName));
    }
}
